package name.fowler.refactoring.simplestore;

import java.util.ArrayList;
import java.util.List;

public class Customer {
	private String name;
	private List<Rental> rentals = new ArrayList<Rental>();
	private Logger logger;

	public Customer(String name) {
		this.name = name;
	}

	public void setLogger(Logger logger){
		this.logger = logger;
	}
	
	public String getName() {
		return name;
	}

	public void addRental(Rental arg){
		rentals.add(arg);
	}

	public String getStatement() {
		double totalAmount = 0;
		int frequentRenterPoints = 0;
				
		String result = "Rental record for " + name + "\n";
		
		for(Rental each: rentals) {
			// Determine amounts for each line
			double amount = computeAmount(each);

			// Add frequent renter points
			frequentRenterPoints++;

			// Add bonus for a two-day new-release rental
			if ((each.getMovie().getPriceCode() == PriceCodes.NewRelease) && (each.getDaysRented() > 1)) {
				frequentRenterPoints ++;
			}

			// Show figures for this rental
			result += "\t" + each.getMovie().getTitle() + "\t" + amount + "\n";
			totalAmount += amount;
		}

		// Add footer lines
		result += "Amount owed is " + totalAmount + "\n";
		result += "You earned " + frequentRenterPoints + " frequent renter points.";
		
//		logger.log(result);
		return result;
	}

    private double computeAmount(Rental rental) {
        double amount = 0;
        switch(rental.getMovie().getPriceCode()) {
            case Regular:
                amount += 2;
                if (rental.getDaysRented() > 2) {
                    amount += (rental.getDaysRented() - 2) * 1.5;
                }
                break;

            case NewRelease:
                amount += rental.getDaysRented() * 3;
                break;

            case Children:
                amount += 1.5;
                if (rental.getDaysRented() > 3) {
                    amount = (rental.getDaysRented() - 3) * 1.5;
                }
                break;
        }

        return amount;
    }
}

